# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS

from com.zzh.dao.projectPublicityDao import ProjectPublicityAPI

import time

from . import app

CORS(app, supports_credentials=True)

local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''立项公示表 zzh_project_publicity API'''


# -----------        姚师傅专用昏割线        -----------------------------------------------------------
@app.route('/query-project-publicity/<int:index>',
           methods=['POST'])  # 查询zzh_project_publicity表。每次根据index的不同返回第1-10条，11-20条，21-30条数据（如果有）
def queryProjectPublishcity(index):
    projectPublicityKeyword = request.json.get('projectPublicityKeyword')

    print projectPublicityKeyword

    if projectPublicityKeyword:
        queryResultOfDatabaseProjectPublicity, projectPublicityListJson = ProjectPublicityAPI().queryProjectPublicity(
            index, projectPublicityKeyword)  # 返回值的获取顺序要匹配
    else:
        queryResultOfDatabaseProjectPublicity, projectPublicityListJson = ProjectPublicityAPI().queryProjectPublicityAll(
            index)  # 返回值的获取顺序要匹配

    if queryResultOfDatabaseProjectPublicity:
        return jsonify({"message": {"code": 1, "info": "已查询到公示信息"}, "hotsearchListJson": projectPublicityListJson})
    else:

        return jsonify({"message": {"code": 0, "info": "未查询到公示信息"}})


@app.route('/add-project-publicity', methods=['POST'])  # invoice表添加记录
def addProjectpublicity():
    if not request.json:
        print request.json
        abort(400)
    addPP = {
        'publicityTitle': request.json.get('publicityTitle', ""),  # itemId  关联zzh_item表item_id字段,默认关联第一条
        'departId': request.json.get('departId', 1),  # departId关联zzh_depart表  depart_id字段，默认关联第一条
        'publishTime': request.json.get('publishTime', local_time),  # 公示发布时间
        'publicityUrl': request.json.get('publicityUrl', ""),  #
        'publishStatus': request.json.get('publishStatus', 0),  # 发布状态，默认0：未审核，1：已审核，-1：已删除
        'publishRemark': request.json.get('publishRemark', "")  # 备注
    }

    addPPResult = ProjectPublicityAPI().addPP(addPP)
    if addPPResult:
        return jsonify({"addProjectPublicityInfo": addPP,
                        "massage": {"code": 1, "info": "addProjectPublicity OK"}})  # 添加函数调用成功，返回添加的信息（不包括hotsearch_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addProjectPublicity Failed"}})  # 添加函数调用失败


@app.route('/update-project-publicity/<int:PPId>', methods=['POST'])  # 根据hotsearchId更新zzh_hotsearch表指定记录
def updatePPById(PPId):
    if not request.json:
        print request.json
        abort(400)
    updatePP = {
        'publicityTitle': request.json.get('publicityTitle', ""),  # itemId  关联zzh_item表item_id字段,默认关联第一条
        'departId': request.json.get('departId', 1),  # departId关联zzh_depart表  depart_id字段，默认关联第一条
        'publishTime': request.json.get('publishTime', local_time),  # 公示发布时间
        'publicityUrl': request.json.get('publicityUrl', ""),  #
        'publishStatus': request.json.get('publishStatus', 0),  # 发布状态，默认0：未审核，1：已审核，-1：已删除
        'publishRemark': request.json.get('publishRemark', "")  # 备注

    }
    addPPResult = ProjectPublicityAPI().updatePPById(updatePP, PPId)
    if addPPResult:
        return jsonify({"updateProjectPublicityInfo": updatePP,
                        "massage": {"code": 1,
                                    "info": "updateProjectPublicity OK"}})  # 添加函数调用成功，返回添加的信息（不包括hotsearch_id）
    else:
        return jsonify({"message": {"code": 0, "info": "updateProjectPublicity Failed"}})  # 添加函数调用失败


@app.route('/delete-project-publicity/<int:PPId>', methods=['DELETE'])  # 根据hotsearchId删除zzh_hotsearch表指定记录
def deletePPById(PPId):
    deletePPResult = ProjectPublicityAPI().deletePPById(PPId)
    if deletePPResult:
        return jsonify({"message": {"code": 1, "info": "删除项目公示记录操作成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除项目公示记录操作失败！"}})
